<?php
use Laravel\Cache;

use Laravel\Config;

class Category_Admin_Home_Controller extends Admin_Controller {

	public $restful = true;

    public function __construct()
    {
        parent::__construct();
        //--Your Code --//
        $this->theme->set_theme("admin") ;

    }

	public function get_index($pid=0){
        $data['pid'] = $pid;
        $data['parent'] = Categories::where('parent_id','=',$pid)->get(); 

        //dd($data['parent']);

        $this->theme->title("Category List");
        return $this->theme->render('category::admin.index',$data);
	}



    public function get_child($pid=0){
        $data['pid'] = $pid;
        $data['parent'] = Categories::where('parent_id','=',$pid)->get(); 
        $this->theme->title("Category List");
        return $this->theme->render('category::admin.index',$data);
    }

    public function get_create($pid=0){

        $p = DB::table('categories')->where('id', '=', $pid)->first();

        if($p){
            $data['parent_name'] = $p->name;
            $data['parent_id'] = $p->id;
        }else{
            $data['parent_name'] = 'Root Category';
            $data['parent_id'] = 0;
        }

        $data['caticon'] = Config::get('Category::config.caticon');
        
        //dd($data['parent_name']);
        $this->theme->title("Admin:Create Category");
        return $this->theme->render('category::admin.create',$data);
    }

    public function post_create($pid=0){

        $rules = array(
            'parent_id' => 'required|integer',
            'parent_name' => 'required',
            'name' => 'required|min:3|max:100',
        );


        $validator = Validator::make(Input::get(), $rules);
        if( $validator->fails() )
        {
            return Redirect::back()->with_input()->with_errors($validator)->with('message','Invalid Operation !');
        }
        else
        {
            $data = array(

                'parent_id' => Input::get('parent_id'),
                'name' => Input::get('name'),
                'slug' => Str::slug(Input::get('name')),
            	'icon' => Input::get('category_icon'),	
                'description' => Input::get('description'),
                'meta_title' => Input::get('meta_title'),
                'meta_keyword' => Input::get('meta_keyword'),
                'meta_description' => Input::get('meta_description')

            );
            
          Caches::clear('categories');
          
          Hierarchy::insert('categories',$data);

            //return Redirect::to('admin/home/child/'.Input::get('parent_id'));
            return Redirect::back()->with('message','Data Successfully inserted !');
        }
    }

    public function get_edit($pid){

        $p = DB::table('categories')->where('id', '=', $pid)->first();
       //dd($p);

        $data['cat']['info'] = $p;

        if($p){
            $data['parent_name'] = $p->name;
            $data['parent_id'] = $p->id;
        }else{
            $data['parent_name'] = 'Root Category';
            $data['parent_id'] = 0;
        }
        
        $data['caticon'] = Config::get('Category::config.caticon');
        //dd($data['parent_name']);
        $this->theme->title("Admin:Create Category");
        return $this->theme->render('category::admin.edit',$data);

    }

    public function post_edit(){

        $rules = array(

            'parent_id' => 'required|integer',
            'parent_name' => 'required',
            'name' => 'required|min:3|max:100',
        );




        $validator = Validator::make(Input::get(), $rules);


        $thisOrderID = Input::get('catID');





        if( $validator->fails() )
        {
            return Redirect::back()->with_input()->with_errors($validator)->with('message','Invalid Operation !');
        }
        else
        {




        	Caches::clear('categories');

            /*Update Query : update category info : by rahul(09/09/14) */
            $categories =DB::table('categories')
                ->where('id', '=', $thisOrderID)
                ->update(array(
                        'name' => Input::get('name'),
                        'slug' => Str::slug(Input::get('name') ),
                		'icon' => Input::get('category_icon'),
                        'description' => Input::get('description'),
                        'meta_title' =>Input::get('meta_title'),
                        'meta_keyword' =>Input::get('meta_keyword'),
                        'meta_description' => Input::get('meta_description'),

                    )
                );



             //return Redirect::to('admin/home/child/'.Input::get('parent_id'));
            return Redirect::back()->with('message','<div class="alert alert-info">Data Successfully updated !</div>');
        }

    }

    public function get_delete($pid){

       // Categories::find($pid)->delete();

       Hierarchy::delete('categories',$pid,true);

     //   DB::table('categories')->where('id', '=', $pid)->delete();
        return Redirect::back()->with('message','Delete Successfully !');

    }
	
}
